<template>
  <!-- 动态粒子背景 -->
  <div class="bg-canvas">
    <canvas id="particleCanvas"></canvas>
  </div>

  <!-- 前景内容 -->
  <div class="center-box">
    <!-- ① 欢迎区 -->
    <h1 class="title">Vue 商城</h1>
    <p class="subtitle">发现最优质的商品，享受最佳的购物体验</p>
    <el-button type="primary" size="large" @click="$router.push('/products')">
      立即购物
    </el-button>

    <!-- ② 为什么选择我们 -->
    <h2 class="section-title">为什么选择我们</h2>
    <div class="why-grid">
      <div class="why-item glass-card">
        <h3>精选商品</h3>
        <p>我们精心挑选每一件商品，确保品质优良</p>
      </div>
      <div class="why-item glass-card">
        <h3>快速配送</h3>
        <p>24小时内发货，全国范围内快速送达</p>
      </div>
      <div class="why-item glass-card">
        <h3>品质保证</h3>
        <p>100%正品保证，不满意可退换</p>
      </div>
      <div class="why-item glass-card">
        <h3>贴心服务</h3>
        <p>24/7客户服务，随时为您解答疑问</p>
      </div>
    </div>

    <!-- ③ 热门产品 -->
    <h2 class="section-title">热门产品</h2>
    <el-row :gutter="40">
      <el-col :xs="24" :sm="12">
        <el-card shadow="hover" class="hot-card glass-card">
          <img src="https://picsum.photos/seed/phone/400/400" class="hot-img" />
          <div class="hot-info">
            <div class="name">智能手机</div>
            <div class="price">￥2999</div>
            <el-button type="primary" @click="$router.push('/product/1')">
              查看详情
            </el-button>
          </div>
        </el-card>
      </el-col>
      <el-col :xs="24" :sm="12">
        <el-card shadow="hover" class="hot-card glass-card">
          <img src="https://picsum.photos/seed/earphone/400/400" class="hot-img" />
          <div class="hot-info">
            <div class="name">无线耳机</div>
            <div class="price">￥599</div>
            <el-button type="primary" @click="$router.push('/product/2')">
              查看详情
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'

/* ===== 粒子背景 ===== */
onMounted(() => {
  const canvas = document.getElementById('particleCanvas')
  const ctx = canvas.getContext('2d')
  let w = (canvas.width = window.innerWidth)
  let h = (canvas.height = window.innerHeight)

  const particles = []
  const count = 80
  for (let i = 0; i < count; i++) {
    particles.push({
      x: Math.random() * w,
      y: Math.random() * h,
      r: Math.random() * 1.5 + 0.5,
      vx: (Math.random() - 0.5) * 0.3,
      vy: (Math.random() - 0.5) * 0.3,
    })
  }

  function animate() {
    ctx.clearRect(0, 0, w, h)
    particles.forEach((p) => {
      ctx.beginPath()
      ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2)
      ctx.fillStyle = 'rgba(255,255,255,0.6)'
      ctx.fill()
      p.x += p.vx
      p.y += p.vy
      if (p.x < 0 || p.x > w) p.vx *= -1
      if (p.y < 0 || p.y > h) p.vy *= -1
    })
    requestAnimationFrame(animate)
  }
  animate()

  window.addEventListener('resize', () => {
    w = canvas.width = window.innerWidth
    h = canvas.height = window.innerHeight
  })
})
</script>

<style scoped>
/* ===== 背景层 ===== */
.bg-canvas {
  position: fixed;
  inset: 0;
  z-index: -1;
  background: linear-gradient(135deg, #42b883 0%, #2c3e50 100%);
}
#particleCanvas {
  display: block;
}

/* ===== 前景层 ===== */
.center-box {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  text-align: center;
  padding: 60px 20px;
  color: #fff;
}

/* 标题 */
.title {
  font-size: 48px;
  margin-bottom: 16px;
  letter-spacing: 2px;
}
.subtitle {
  font-size: 20px;
  opacity: 0.85;
  margin-bottom: 32px;
}
.section-title {
  font-size: 36px;
  margin: 80px 0 40px;
}

/* 毛玻璃卡片公共样式 */
.glass-card {
  background: rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  transition: 0.3s;
}
.glass-card:hover {
  transform: translateY(-4px);
  background: rgba(255, 255, 255, 0.18);
}

/* 为什么选择我们 */
.why-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 24px;
  width: 100%;
  max-width: 1000px;
}
.why-item {
  padding: 24px;
}
.why-item h3 {
  font-size: 20px;
  margin-bottom: 8px;
}
.why-item p {
  font-size: 14px;
  opacity: 0.85;
  line-height: 1.6;
}

/* 热门产品 */
.hot-card {
  border-radius: 16px;
  overflow: hidden;
}
.hot-img {
  width: 100%;
  aspect-ratio: 1/1;
  object-fit: cover;
}
.hot-info {
  padding: 20px;
}
.hot-info .name {
  font-size: 20px;
  margin-bottom: 8px;
}
.hot-info .price {
  color: #f56c6c;
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 16px;
}
</style>